/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------

*/
/* @author gfast */

package tests.gfast;

import java.lang.Thread;
import java.io.File;
import com.webdeninteractive.toolkit.FileTools;
import com.webdeninteractive.bie.listeners.directorywatcher.XDirectoryListener;
import com.webdeninteractive.bie.listeners.directorywatcher.DirectoryWatcher;
import com.webdeninteractive.bie.listeners.directorywatcher.DirectoryListener;

public class FileSystemListenerTest {

    public static void main(String[] args) {
        if (args.length < 1) {
            System.err.println("Gimme a watch directory, argh!");
            System.exit(-1);
        }
        String watchDirectory = args[0];
        int watchInterval = 3000;

        System.out.println("Watching " + watchDirectory);
        if (! FileTools.checkAndCreate(watchDirectory) ) {
            System.err.println("Barf on dir");
            System.exit(-1);
        }

        try {

            DirectoryWatcher dw =
                new DirectoryWatcher(watchDirectory, watchInterval);
            dw.addDirectoryListener( new DirectoryListener() {

                    public void elementAdded( File f ) {
                        System.out.println("New: " + f.getPath());
                    }
    
                    public void elementRemoved( File f ) {
                        System.out.println("Rem: " + f.getPath());
                    }
    
                    public void elementChanged( File f ) {
                        System.out.println("Chg: " + f.getPath());
                    }

                    public void terminated( String message, Exception e ) {
                        System.out.println("ERROR (thread terminated):");
                        System.out.println(message);
                        if (e!=null) e.printStackTrace();
                        System.exit(-1);
                    }

                });
            dw.startWatching(true);

        } catch ( Exception e ) {
            e.printStackTrace();
            System.exit(-1);
        }
        
        try {
            while( true ) {
                Thread.sleep(10);
            }
        } catch (Exception e) {
            // ignore and quit
        }
    }


}



 
